package com.qmth.gkwb.web.view;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qmth.gkwb.data.document.Student;
import com.qmth.gkwb.data.exception.SyncErrorException;
import com.qmth.gkwb.data.repository.StudentRepo;
import com.qmth.gkwb.data.sync.SyncService;

@Controller
@RequestMapping("view/sync")
public class SyncController {

	@Autowired private StudentRepo studentRepo;
	@Autowired private SyncService syncService;
	
	
	@RequestMapping(method=RequestMethod.GET)
	@ResponseBody
	public int syncOne(@RequestParam String examId){
		int synced = 0;
		List<Student> students = studentRepo.findByExamIdAndIsSycAndSycFail(examId, false,false);
		if(students.size() > 0){
			Student student = students.get(0);
			try {
				boolean result = syncService.syncStudent(student);
				syncService.syncExam(student);
				if(result){
					synced++;
					//更新考生同步状态为true
					student.setIsSyc(true);
				}
				
			} catch (Exception e) {
				student.setSycFail(true);
				e.printStackTrace();
			}finally{
				studentRepo.save(student);
				
			}
		}
		return synced;
	}
}
